package com.example.backend.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.common.Result;
import com.example.backend.mapper.SysUserMapper;
import com.example.backend.pojo.SysUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户管理
 */
@RestController//用来返U回json数据
@RequestMapping("/sysUser") //统一的路由,给接口加上一个统一的前缀

public class SysUserController {
    //引入Mapper,快熟实现，但不规范，规范是通过service层引入
    @Resource
    SysUserMapper sysUserMapper;

    //后端登录接口（与前端login对应）
    @PostMapping("/login")
    public Result<?> login(@RequestBody SysUser sysUser) {
        //根据前台传过来的用户名和密码登录查询出数据库中与之匹配的数据，如果对象res存在，
        SysUser res =  sysUserMapper.selectOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, sysUser.getUsername()).eq(SysUser::getPassword, sysUser.getPassword()));
        if(res == null) {
            return Result.error("-1", "用户名或者密码错误");
        }
        return Result.success();
    }

    //后端注册接口
    @PostMapping("/register")
    public Result<?> register(@RequestBody SysUser sysUser) {
        //根据前台传过来的用户名和密码登录查询出数据库中与之匹配的数据，如果对象res存在，
        SysUser res =  sysUserMapper.selectOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, sysUser.getUsername()));
        if(res != null) {//判断用户名是否存在，存在提示“重复”
            return Result.error("-1", "用户名重复");
        }
        if(sysUser.getPassword() == null) {//如果密码为空,则赋予其一个默认密码
            sysUser.setPassword("123456");
        }
        sysUserMapper.insert(sysUser);
        return Result.success();
    }


    @PostMapping
    public Result<?> save(@RequestBody SysUser sysUser) {
        if (sysUser.getPassword() == null) {
            sysUser.setPassword("123456");
        }
        sysUserMapper.insert(sysUser);
        return Result.success();
    }

    //调用updateById更新方法进行更新操作
    @PutMapping
    public Result<?> update(@RequestBody SysUser sysUser) {

        sysUserMapper.updateById(sysUser);
        return Result.success();
    }

    //{id}：{}占位符的方式传入参数；通过@PathVariable注解引入参数，当有多个参数时，就需要，加多个注解以及参数
    //如:@DeleteMapping("/{id}/{name}")
    //delete(@PathVariable Long id,@PathVariable string name)
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {

        sysUserMapper.deleteById(id);
        return Result.success();
    }


    //根据用户姓名模糊查询
    //pageNum:当前页面，当前翻到的是第几页，前往第几页
    //pageSize:每页有多少条
    //search:前端定义的查询的关键字
    //可通过lambdaQuery()来生成一个Wrapper对象，给lambdaQuery()一个泛型
    //StrUtil:hutool的工具库，一个值的空值判断，判断某个值不为空值
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<SysUser> wrapper = Wrappers.<SysUser>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
            wrapper.like(SysUser::getUsername, search);
        }
        Page<SysUser> sysUserPage = sysUserMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(sysUserPage);
    }

}
